package com.cvit.service;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.cvit.dao.StudentMapper;
import com.cvit.model.Student;
import com.cvit.utils.ExcelUtil;
import com.cvit.utils.ExcelUtils;
import com.cvit.utils.Utils;

@Service
public class StudentService {

	@Autowired
	StudentMapper studentMapper;

	/**
	 * 查询所有
	 * */
	public List<Student> getAll() {
		List<Student> student = studentMapper.selectByExample(null);
		return student;
	}
	
	/**
	 * 查询所有
	 * */
	public List<Student> getOne(Student student) {
		List<Student> students = studentMapper.selectOne(student);
		return students;
	}
	
	/**
	 * 微信登录
	 * */
	public List<Student> stuLogin(Student student) {
		List<Student> students = studentMapper.stuLogin(student);
		return students;
	}
	
	/**
	 * 根据id查询
	 * */
	public Student getStudentById(String id){
		Student student = studentMapper.selectByPrimaryKey(id);
		return student;
	}
	
	/**
	 * 查询考生号是否存在
	 * */
	public List<Student> selectByExamineeNumber(Student student){
		List<Student> stuNumber = studentMapper.selectByExamineeNumber(student);
		return stuNumber;
	}
	
	/**
	 * 查询身份证号是否存在
	 * */
	public List<Student> selectByCardNumber(Student student){
		List<Student> stuNumber = studentMapper.selectByCardNumber(student);
		return stuNumber;
	}

	/**
	 * 根据考生号、姓名、身份证号查询
	 * */
	public List<Student> getStudentBys(Student student) {
		List<Student> studentL = studentMapper.selectBys(student);
		return studentL;
	}
	
	/**
	 * 添加信息
	 * */
	public void addStudent(Student student){
		student.setId(Utils.getsUUID());
		student.setPayType(2);
		studentMapper.insert(student);
	}
	
	/**
	 * 修改信息
	 * */
	public void updateStudent(Student student){
		if(student.getPayType()==null){
			studentMapper.updateByPrimaryKeySelective(student);
		}else{
			student.setPayTime(new Date());
			studentMapper.updateByPrimaryKeySelective(student);
		}
	}
	
	/**
	 * 根据ID删除信息
	 * */
	public void deleteStudent(String id){
		studentMapper.deleteByPrimaryKey(id);
	}
	
	
	/**
	 * 修改缴费状态
	 * */
	public void updatePayType(String id){
		studentMapper.updatepayTypeById(id);
	}
	
	/**
	 * 导入Excel
	 * */
	public void importExcelInfo(InputStream in, MultipartFile file) throws Exception{
	    List<List<Object>> listob = ExcelUtils.getBankListByExcel(in,file.getOriginalFilename()); 
	    System.out.println("listob----"+listob);
	    for (int i = 0; i < listob.size(); i++) {
	        List<Object> ob = listob.get(i);
	        System.out.println("ob----"+ob);
	        if(ob.size()==6){
	        	Student student = new Student();
		        student.setId(Utils.getsUUID());
		        student.setCardId(String.valueOf(ob.get(0)));
		        student.setExamineeId(String.valueOf(ob.get(1)));
		        student.setName(String.valueOf(ob.get(2)));
		        student.setSchool(String.valueOf(ob.get(3)));
		        student.setMajor(String.valueOf(ob.get(4)));
		        	student.setDirection(String.valueOf(ob.get(5)));
		        student.setPayType(2);
		        studentMapper.insert(student);
	        }else{
	        	Student student = new Student();
		        student.setId(Utils.getsUUID());
		        student.setCardId(String.valueOf(ob.get(0)));
		        student.setExamineeId(String.valueOf(ob.get(1)));
		        student.setName(String.valueOf(ob.get(2)));
		        student.setSchool(String.valueOf(ob.get(3)));
		        student.setMajor(String.valueOf(ob.get(4)));
		        student.setDirection("");
		        student.setPayType(2);
		        studentMapper.insert(student);
	        }
			
	    }  
	}
}
